package com.ajview.core.config.model;

import com.ajview.common.core.factory.YamlPropertySourceFactory;
import com.ajview.common.utils.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author ZhangJunJie
 * @Date 2022-04-29
 * <p>
 * 概要：映射核心配置文件的JWTToken模型
 */
@PropertySource(value = "classpath:config/ajview-core-config.yml", factory = YamlPropertySourceFactory.class, encoding = "UTF-8")
@ConfigurationProperties(prefix = "token.config")
@Component
public class JwtTokenModel {


    private static final String DEF_SECRET_VAL = "JXU1QzBGJXU3RUEy";
    private static final long DEF_EXPIRATION_MINUTE = 60;
    private static final String DEF_HEADER_NAME = "Authorization";
    private static final long DEF_LIMIT = 30;
    /**
     * 令牌秘钥 长度必须大于10
     */
    private String secret;
    /**
     * 多久后过期。单位分钟
     */
    private long expirationMinute;
    /**
     * 从请求头的哪里获取token
     * 如：Authorization 和 token
     */
    private String header;
    /**
     * 续费时间 单位分钟
     * 当token剩余时间小于该时间自动刷新token续约
     */
    private long limit;

    public String getSecret() {
        return StringUtils.isEmpty(this.secret) ? DEF_SECRET_VAL : this.secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    /**
     * set 分钟 get毫秒
     *
     * @return
     */
    public long getExpirationMinute() {
        return this.expirationMinute <= 0 ? (DEF_EXPIRATION_MINUTE * 60000) : (this.expirationMinute * 60000);
    }

    /**
     * set 分钟 get毫秒
     *
     * @return
     */
    public void setExpirationMinute(long expirationMinute) {
        this.expirationMinute = expirationMinute;
    }

    public String getHeader() {
        return StringUtils.isEmpty(this.header) ? DEF_HEADER_NAME : this.header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    /**
     * set 分钟 get毫秒
     * 当token剩余时间小于该时间自动刷新token续约
     *
     * @return
     */
    public long getLimit() {
        return this.limit <= 0 ? (DEF_LIMIT * 60000) : (this.limit * 60000);
    }

    /**
     * set 分钟 get毫秒
     * 当token剩余时间小于该时间自动刷新token续约
     *
     * @return
     */
    public void setLimit(long limit) {
        this.limit = limit;
    }
}
